package com.example.myapplication.Room.room.manager;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

//import com.example.myapplication.Room.room.Student;
import com.example.myapplication.Room.room.Student;
import com.example.myapplication.Room.room.StudentDao;
import com.example.myapplication.Room.room.StudentDataBase;

import java.util.List;

///DB的引擎
public class DBEngine {
    private StudentDao studentDao;

    public DBEngine(Context context){
        StudentDataBase studentDataBase = StudentDataBase.getInstance(context);
        studentDao = studentDataBase.getStudentDao();
    }

    ///Dao 的相关操作
    ///插入
    public void insertStudents(Student...students){
        Log.i("TAG", "insertStudents-: -------------------0");
        new InsertAsyncTask(studentDao).execute(students);
    }
    ///更新
    public void updateStudents(Student...students){
        new UpdateAsyncTask(studentDao).execute(students);
    }
    ///删除
    public void deleteStudents(Student...students){
        new DeleteAsyncTask(studentDao).execute(students);
    }

    ///删除全部
    public void deleteAllStudents(){
        new DeleteAllAsyncTask(studentDao).execute();
    }

    ///查找
    public void getAllStudents(Student...students){
        new GetAllAsyncTask(studentDao).execute();
    }

    ///异步操作
    static class InsertAsyncTask extends AsyncTask<Student,Void,Void>{
        private StudentDao dao;
        public InsertAsyncTask(StudentDao studentDao){
            dao = studentDao;
        }
        @Override
        protected Void doInBackground(Student... students) {
            dao.insertStudent(students);
            return null;
        }
    }

    static class UpdateAsyncTask extends AsyncTask<Student,Void,Void>{
        private StudentDao dao;
        public UpdateAsyncTask(StudentDao studentDao){
            dao = studentDao;
        }
        @Override
        protected Void doInBackground(Student... students) {
            dao.upDateStudent(students);
            return null;
        }
    }

    static class DeleteAsyncTask extends AsyncTask<Student,Void,Void>{
        private StudentDao dao;
        public DeleteAsyncTask(StudentDao studentDao){
            dao = studentDao;
        }
        @Override
        protected Void doInBackground(Student... students) {
            dao.deleteStudent(students);
            return null;
        }
    }

    static class DeleteAllAsyncTask extends AsyncTask<Void,Void,Void>{
        private StudentDao dao;
        public DeleteAllAsyncTask(StudentDao studentDao){
            dao = studentDao;
        }

        @Override
        protected Void doInBackground(Void... voids) {
            dao.deleteAllStudent();
            return null;
        }
    }

    static class GetAllAsyncTask extends AsyncTask<Void,Void,Void>{
        private StudentDao dao;
        public GetAllAsyncTask(StudentDao studentDao){
            dao = studentDao;
        }
        @Override
        protected Void doInBackground(Void... voids) {
            List<Student> studentList= dao.getAllStudent();
            Log.i("TAG", "doInBackground: "+studentList.size());
            for(Student student:studentList){
                Log.i("TAG", "doInBackground: "+student.toString());
            }
            return null;
        }
    }


}
